//
//  ViewController.m
//  GCD常用函数
//
//  Created by 李煜 on 2024/3/20.
//

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

-(void)viewDidLoad {
	[super viewDidLoad];
	
    [self methodName2];
}
- (void)methodName2 {
    NSOperationQueue *q1 = [[NSOperationQueue alloc]init];
    
    NSBlockOperation *b1 = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i< 1000; i++) {
            NSLog(@"-----任务1--%d",i);
        }
    }];
    NSBlockOperation *b2 = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i< 1000; i++) {
            NSLog(@"-----任务2--%d",i);
        }
    }];
    NSBlockOperation *b3 = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i< 1000; i++) {
            NSLog(@"-----任务3--%d",i);
        }
    }];
    
    [b3 addDependency:b1];
    [b3 addDependency:b2];
    
    [q1 addOperation:b1];
    [q1 addOperation:b2];
    [q1 addOperation:b3];
}
- (void)methodName1 {
    NSOperationQueue *queue1 = [[NSOperationQueue alloc]init];
    NSOperationQueue *queue2 = [[NSOperationQueue alloc]init];
    
    NSBlockOperation *op1= [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"--down1--%@",[NSThread currentThread]);
    }];
    NSBlockOperation *op2= [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"--down2--%@",[NSThread currentThread]);
    }];
    NSBlockOperation *op3= [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"--down3--%@",[NSThread currentThread]);
    }];
    NSBlockOperation *op4= [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"--down4--%@",[NSThread currentThread]);
    }];
    
    
    [op1 addDependency:op2];
    [op2 addDependency:op3];
    [op3 addDependency:op4];
    
    //***注意1: 操作依赖可以跨队列依赖
    
    
    //队列1添加op1 op2 op3
    [queue1 addOperation:op1];
    [queue1 addOperation:op2];
    [queue1 addOperation:op3];
    //队列2添加op4
    [queue2 addOperation:op4];
    [queue1 addOperationWithBlock:^{
        NSLog(@"--down5--%@",[NSThread currentThread]);
    }];
    
    
    //***操作监听
    //**注意2:op3任务线程和op3的completion不一定同一个线程,同时不能保证completion立即在op3任务完成后调用,只能保证在op3任务之后,因为都是并发执行.
    op3.completionBlock = ^{
        NSLog(@"--completion--%@",[NSThread currentThread]);
    };
}
@end
